using System.Collections.Generic;
using NyMPH.Persistence;
using NyMPH.Persistence.DSNyMPHTableAdapters;
using System.Data.Linq;
using NyMPH.Patches;
using System;
using System.Data.SqlServerCe;
namespace NyMPH.Persistence
{
    public class Database
    {
        /// <summary>
        /// Kludge used before UI can set database location.
        /// </summary>
        private const string DB_PATH = @"NyMPH_Database.sdf"; //use same directory as executable

        /// <summary>
        /// Connection string template for the NyMPH database. 
        /// </summary>
        private const string CONN_TEMPLATE = @"Data Source={0};Persist Security Info=False;";

        //initially set to the same directory as the executable
        private string _dbPath = DB_PATH;

        //shared Dataset instance
        private DSNyMPH _ds;

        private TableAdapterManager _tam;


        #region Properties

        /// <summary>
        /// The location of the NyMPH Database file.
        /// </summary>
        public string DatabasePath
        {
            set
            {
                _dbPath = value;
            }
            get
            {
                return _dbPath;
            }
        }
        /// <summary>
        /// The Connection String for the NyMPH Database.
        /// </summary>
        public string ConnectionString
        {
            get
            {
                return String.Format(CONN_TEMPLATE, _dbPath);
            }
        }

        /// <summary>
        /// A typed dataset representing the NyMPH Database.
        /// </summary>
        public DSNyMPH NyMPHData
        {
            get
            {
                return _ds;
            }
        }


        public TableAdapterManager NyMPHTableAdapterManager
        {
            get
            {
                return _tam;
            }
            private set
            {
                _tam = value;
            }
        }



        #endregion

        #region Constructors

        /// <summary>
        /// Create a new instance of the NyMPH Database class to wrap the persistence layer
        /// </summary>
        public Database()
        {
            //init data objects
            _ds = new DSNyMPH();

            SqlCeConnection _conn = new SqlCeConnection(this.ConnectionString);

            _tam = new TableAdapterManager();

//            _tam.Connection = _conn;

            _tam.tbKeywordRelationshipsTableAdapter = new tbKeywordRelationshipsTableAdapter();
            _tam.tbKeywordRelationshipsTableAdapter.Fill(_ds.tbKeywordRelationships);

            _tam.tbKeywordsInPatchTableAdapter = new tbKeywordsInPatchTableAdapter();
            _ds.EnforceConstraints = false;
            
            _tam.tbKeywordsInPatchTableAdapter.Fill(_ds.tbKeywordsInPatch);
           // _ds.EnforceConstraints = true;

            _tam.tbKeywordTableAdapter = new tbKeywordTableAdapter();
            _tam.tbKeywordTableAdapter.Fill(_ds.tbKeyword);

            _tam.tbPatchBanksTableAdapter = new tbPatchBanksTableAdapter();
            _tam.tbPatchBanksTableAdapter.Fill(_ds.tbPatchBanks);

            _tam.tbPatchDataTableAdapter = new tbPatchDataTableAdapter();
            _tam.tbPatchDataTableAdapter.FillLight(_ds.tbPatchData);

            _tam.tbPatchDataVersionsTableAdapter = new tbPatchDataVersionsTableAdapter();
            _tam.tbPatchDataVersionsTableAdapter.Fill(_ds.tbPatchDataVersions);

            _tam.tbPatchesInBankTableAdapter = new tbPatchesInBankTableAdapter();
            _tam.tbPatchesInBankTableAdapter.Fill(_ds.tbPatchesInBank);

            _tam.tbPatchesTableAdapter = new tbPatchesTableAdapter();
            _tam.tbPatchesTableAdapter.Fill(_ds.tbPatches);


            KeywordDAL = new KeywordDataHelper(this.NyMPHTableAdapterManager.tbKeywordTableAdapter);
            


        }


        #endregion

        #region DAL Helpers

        public PatchesDataHelper PatchesDAL;
        public KeywordDataHelper KeywordDAL;




        #endregion



        #region Public Methods

        public bool Save(IPersistence Item)
        {
            // TODO add implementation and return statement
            throw new NotImplementedException("TODO:");
        }
        public bool Delete(IPersistence Item)
        {
            // TODO add implementation and return statement
            throw new NotImplementedException("TODO:");
        }
        public void Load(int OID, out IPersistence Item)
        {
            // TODO add implementation
            throw new NotImplementedException("TODO:");
        }
        public void Find(Criteria Item)
        {
            // TODO add implementation
            throw new NotImplementedException("TODO:");
        }
        public bool Compact()
        {
            // TODO add implementation and return statement
            throw new NotImplementedException("TODO:");
        }
        public List<Patch> GetPatchList(List<Criteria> Criterions)
        {
            // TODO add implementation
            throw new NotImplementedException("TODO:");
        }
        #endregion

        #region Patch Methods
        public bool Save(Patch Item)
        {
            return true;

        }
        public bool Delete(Patch Item)
        {
            // TODO add implementation and return statement
            throw new NotImplementedException("TODO:");
        }
        public void Load(int OID, out Patch Item)
        {
            // TODO add implementation
            throw new NotImplementedException("TODO:");
        }





        #endregion

    }
}


